package com.miplus.ccrm.core.service.cust;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.cust.ContEditVo;
import com.miplus.ccrm.core.controller.vo.cust.ContReqVo;
import com.miplus.ccrm.core.controller.vo.cust.ContResVo;
import com.miplus.ccrm.core.pojo.ConContact;
import com.miplus.ccrm.core.mapper.ConContactMapper;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.conf.SysNamesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【con_contact(客户联系人表)】的数据库操作Service实现
* @createDate 2024-03-30 09:55:28
*/
@Service
public class ConContactServiceImpl extends MPJBaseServiceImpl<ConContactMapper, ConContact>
    implements ConContactService{

    @Value("${aes.key}")
    private String aesKey;

    @Resource
    private SysNamesService sysNamesService;

    @Override
    public IPage<ContResVo> contactList(JSysUser user, ContReqVo paras)
    {
        String $usrid=user.getUsrid();

        List<String> phntypelist=sysNamesService.getNamesID("CON.PHONETYP",false);
        List<String> relationtypelist=sysNamesService.getNamesID("CON.RELATION",false);
        List<String> originlist=sysNamesService.getNamesID("DATA.ORIGIN", JUsrPerm.hasPermission(user,JUsrPerm.VIEW_ALLORGIN));

        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                ContResVo.class,
                new MPJLambdaWrapper<ConContact>()
                        .selectAs("fun_getnames('CON.RELATION',t.relational)",ContResVo::getRelationaldsc)
                        .selectAs("fun_getnames('DATA.ORIGIN',t.origin)",ContResVo::getOrigindsc)
                        .selectAs("fun_getnames('CON.PHONETYP',t.phntype)",ContResVo::getPhntypedsc)
                        .selectAs("fi_hasStopCall(t.phone,'CALL')",ContResVo::getIsstopcall)
                        .selectAs("fi_hasStopCall(t.phone,'SMS')",ContResVo::getIsstopsms)
                        .selectAs(String.format("fi_starSecu('%s','disstar.phone',t.phone,'')",$usrid),ContResVo::getPhone)
                        .select(ConContact::getContactnumber,ConContact::getCustomernumber,ConContact::getName,ConContact::getRelational,
                                ConContact::getOrigin,ConContact::getPhntype,ConContact::getRemark,ConContact::getCreatedby,
                                ConContact::getCreateddate,ConContact::getLastmodifiedby,ConContact::getLastmodifieddate)
                        .in(ConContact::getPhntype,phntypelist)
                        .in(ConContact::getRelational,relationtypelist)
                        .in(ConContact::getOrigin,originlist)
                        .eq("customernumber",paras.getCustomernumber())
        );
    }

    @Override
    public ContResVo contactInfo(int contactid)
    {
        return selectJoinOne(
                ContResVo.class,
                new MPJLambdaWrapper<ConContact>()
                        .selectAs("fun_getnames('CONTACTREL',t.relational)",ContResVo::getRelationaldsc)
                        .selectAs("fun_getnames('DATA.ORIGIN',t.origin)",ContResVo::getOrigindsc)
                        .selectAs("fun_getnames('CON.PHONETYP',t.phntype)",ContResVo::getPhntypedsc)
                        .selectAs("fi_hasStopCall(t.phone,'CALL')",ContResVo::getIsstopcall)
                        .selectAs("fi_hasStopCall(t.phone,'SMS')",ContResVo::getIsstopsms)
                        .selectAll(ConContact.class)
                        .eq("contactnumber",contactid)
        );
    }

    @Override
    public CmdResVo updateContact(String $usrid, ContEditVo paras)
    {
        ConContact def=new ConContact();
        BeanUtils.copyProperties(paras,def);
        String phn=paras.getPhone().trim();
        if(phn.indexOf("*")>=0){
            def.setPhone(null); //保持原号码
        }
        else{
            def.setPhone(phn);
        }
        def.setLastmodifiedby($usrid);
        def.setLastmodifieddate(new Date());
        int ret=baseMapper.updateById(def);
        return new CmdResVo(ret,def.getContactnumber().toString());
    }

    @Override
    public CmdResVo insertContact(String $usrid,ContEditVo paras)
    {
        ConContact def=new ConContact();
        BeanUtils.copyProperties(paras,def);
        if(JPubUtils.IsEmpty(paras.getOrigin())) {
            def.setOrigin("10"); //系统自建
        }
        def.setCreatedby($usrid);
        def.setCreateddate(new Date());
        def.setLastmodifiedby($usrid);
        def.setLastmodifieddate(new Date());
        int ret=baseMapper.insert(def);
        return new CmdResVo(ret,def.getContactnumber().toString());
    }

    @Override
    public CmdResVo deleteContact(int contactid)
    {
        int ret=baseMapper.deleteById(contactid);
        return new CmdResVo(ret,Integer.toString(contactid));
    }

    @Override
    public CmdResVo getSecuPhone(Integer contactid)
    {
        ConContact contact=baseMapper.selectById(contactid);
        String phone=contact.getPhone();
        if(JPubUtils.IsEmpty(aesKey)){
            JPubUtils.PrintWarn("getSecuPhone","nofindaesKey");
        }
        phone=JSecurity.aesEncrypt(phone, aesKey);
        return new CmdResVo(1,phone);
    }

    @Override
    public String getPhone(Integer contactid)
    {
        ConContact contact=baseMapper.selectById(contactid);
        return contact.getPhone();
    }
}




